package com.xiaomi.finance.lc.wx.service;

import java.awt.Color;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.qq.weixin.model.tpl.TemplateSendMsg;
import com.qq.weixin.service.TemplateService;
import com.qq.weixin.util.Constants;

/**
 * Hello world!
 *
 */
public class Demo1 {

	Logger logger = Logger.getLogger(TemplateService.class);
	private CloseableHttpClient httpclient = HttpClients.createDefault();

	public static void main(String[] args) {

		new Demo1().test();
	}

	/**
	 * 建一个模板消息
	 */
	public static TemplateSendMsg createTemplateMsg() {

		String url = "http://staging.h5.jr.mi.com/wx/asset?from=wechat_mifi";
		TemplateSendMsg msg = TemplateSendMsg.create(Constants.TEMPLATE_ID,
				url, Constants.COLOR_BLUE);
		msg.setTouser(Constants.MY_OPENID);
		msg.putDataValue("first",
				TemplateSendMsg.createDataValue("tttttt", Constants.COLOR_BLUE));
		msg.putDataValue("content", TemplateSendMsg.createDataValue(
				"A tpl msg", Constants.COLOR_BLUE));
		msg.putDataValue("time", TemplateSendMsg.createDataValue(
				new Date().toString(), Constants.COLOR_BLUE));
		System.out.println(JSON.toJSONString(msg));
		return msg;
	}

	public static String Color2String(Color color) {
		String R = Integer.toHexString(color.getRed());
		R = R.length() < 2 ? ('0' + R) : R;
		String B = Integer.toHexString(color.getBlue());
		B = B.length() < 2 ? ('0' + B) : B;
		String G = Integer.toHexString(color.getGreen());
		G = G.length() < 2 ? ('0' + G) : G;
		return '#' + R + B + G;
	}

	public void setIndustryId(String id1, String id2) {

		HttpPost post = new HttpPost(
				MessageFormat
						.format("https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token={0}",
								Constants.ACCESS_TOKEN));
		System.out.println(post);

		Map<String, Object> map = new HashMap<String, Object>();
		String json = JSON.toJSONString(map);
		System.out.println(json);
		HttpEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
		post.setEntity(entity);
		CloseableHttpResponse resp = null;
		try {
			resp = httpclient.execute(post);
			String jsonstr = EntityUtils.toString(resp.getEntity());
			System.out.println(jsonstr);

		} catch (Exception e) {
			logger.error(e);
		} finally {
			try {
				resp.close();
			} catch (IOException e) {
				logger.error(e);
			}
		}
	}

	public void test() {

		String url = "http://localhost";

		HttpPost post = new HttpPost(url);
		System.out.println(post);

		TemplateSendMsg msg = createTemplateMsg();

		HttpEntity entity = new StringEntity(JSON.toJSONString(msg),
				ContentType.APPLICATION_JSON);
		post.setEntity(entity);
		System.out.println("entity:" + entity);
		
		System.out.println(post);
		CloseableHttpResponse resp = null;
		try {
			resp = httpclient.execute(post);
			String jsonstr = EntityUtils.toString(resp.getEntity());
			System.out.println("jsonstr: " + jsonstr);

		} catch (Exception e) {
			logger.error(e);
		} finally {
			try {
				resp.close();
			} catch (IOException e) {
				logger.error(e);
			}
		}
	}

}
